﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace CourseSelectionSystem
{
    public partial class FormLogin : Form
    {

        public FormLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 取消登录退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 登录操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if(login())
            {
                //启动计时器
                timer1.Start();

                //隐藏文本内容
                textBoxID.Visible = false;
                textBoxPwd.Visible = false;
                comboBox1.Visible = false;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                buttonCancel.Visible = false;
                buttonLogin.Visible = false;
            }
        }

        private bool login()
        {
            if (textBoxID.Text == "" || textBoxPwd.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("输入信息不完整，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (comboBox1.Text == "管理员")
            {
                if(textBoxID.Text == "adminYXY" && textBoxPwd.Text == "adminYXY")
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }   
            }
            else if (comboBox1.Text == "学生")
            {
                string sql = "select * from Student where ID='" + textBoxID.Text + "'and Password='" + textBoxPwd.Text + "'";
                Database database = new Database();
                IDataReader dr = database.read(sql);
                if(dr.Read())
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            else if (comboBox1.Text == "教师")
            {
                string sql = "select * from Teacher where ID='" + textBoxID.Text + "'and Password='" + textBoxPwd.Text + "'";
                Database database = new Database();
                IDataReader dr = database.read(sql);
                if (dr.Read())
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("输入信息有误，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }


   

        /// <summary>
        /// 图片移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(pictureBox1.Location.X < 200)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X+1, pictureBox1.Location.Y);
            }
            else
            {
                if(comboBox1.Text == "学生")
                {
                    //显示学生选课窗体
                    StudentSelect studentSelect = new StudentSelect();
                    studentSelect.Show();
                    this.Hide();
                }
                else if(comboBox1.Text == "教师")
                {
                    //显示选课情况
                    TeacherView studentView = new TeacherView();
                    studentView.Show();
                    this.Hide();
                }
                else if (comboBox1.Text == "管理员")
                {
                    //显示学生信息窗口
                    AdminView adminView = new AdminView();
                    adminView.Show();
                    this.Hide();

                }
                timer1.Stop();
            }
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {

        }
    }
}
